package org.xtense.rcp.editors.action;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Shell;
import org.xtense.rcp.editors.XmlEditor;
import org.xtense.rcp.editors.util.XmlEntry;
import org.xtense.rcp.util.ImagesContext;

public class ElementPopupActionGroup extends RootPopupActionGroup {
	
	public ElementPopupActionGroup(XmlEditor editor, TreeViewer tv) {
		super(editor, tv);
		menuManager.add(new RemoveAction());
		menuManager.add(new AddBeforeAction());
		menuManager.add(new AddAfterAction());
	}
	
	private class RemoveAction extends Action {
		public RemoveAction() {
			setText("&Remove");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Delete));
		}

		public void run() {
			XmlEntry entry = getSelected();
			entry.remove();
			editor.setDirty(true);
			tv.refresh();
			entry.getNode().detach();
		}
	}
	
	private class AddBeforeAction extends Action {
		public AddBeforeAction() {
			setText("Add Before");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Element));
		}

		@SuppressWarnings("unchecked")
		public void run() {
			XmlEntry entry = getSelected();
			NewDialog d = new NewDialog(new Shell(), NewDialog.Element);
			if (d.open() == InputDialog.OK) {
				XmlEntry newEntry = new XmlEntry(d.getName());
				newEntry.setValue(d.getValue());
				newEntry.setType(XmlEntry.Element);
				entry.addBefore(newEntry);
				editor.setDirty(true);
				
				// add to the model node
				Element ue = DocumentHelper.createElement(newEntry.getName());
				ue.setText(newEntry.getValue());
				int index = entry.getParentEntry().getChildren().indexOf(entry);
				Element base = (Element) ((XmlEntry) entry.getParentEntry())
						.getNode();
				base.elements().add(index - 1, ue);
				
				Element added = (Element) base.elements().get(index - 1);
				newEntry.setNode(added);
			}
			tv.refresh();
		}
	}
	
	private class AddAfterAction extends Action {
		public AddAfterAction() {
			setText("Add After");
			setImageDescriptor(ImagesContext
					.getImageDescriptor(ImagesContext.Element));
		}

		@SuppressWarnings("unchecked")
		public void run() {
			XmlEntry entry = getSelected();
			NewDialog d = new NewDialog(new Shell(), NewDialog.Element);
			if (d.open() == InputDialog.OK) {
				XmlEntry newEntry = new XmlEntry(d.getName());
				newEntry.setValue(d.getValue());
				newEntry.setType(XmlEntry.Element);
				entry.addAfter(newEntry);
				editor.setDirty(true);
				
				// add to the model node
				Element ue = DocumentHelper.createElement(newEntry.getName());
				ue.setText(newEntry.getValue());
				int index = entry.getParentEntry().getChildren().indexOf(entry);
				Element base = (Element) ((XmlEntry) entry.getParentEntry())
						.getNode();
				base.elements().add(index + 1, ue);
				
				Element added = (Element) base.elements().get(index + 1);
				newEntry.setNode(added);
			}
			tv.refresh();
		}
	}
	
}
